import axios from 'axios'
import Router from '@/router'
import store from "@/store"

const service = axios.create({
  baseURL: BASE_URL,
  timeout: 30 * 1000
})
service.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
service.interceptors.request.use(config => {
  const token = Cookies.get('token')
  if (token) {
    config.headers['token'] = token // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
  }
  return config
}, error => {
  Promise.reject(error)
})
const table = {
  "Network Error": "网络错误"
}
service.interceptors.response.use(config => {
  const code = config.status
  if (code !== 200) {
    console.error("error")
  }
  if (config.data.msg === "token不匹配") {
    //  清除 token 
    Cookies.remove("token", {
      path: "/"
    });
    Router.push('/login')
  }
  return config.data
}, error => {
  Promise.reject(error)
})
export default service;
